import {
  delArticleCommentList,
  getArticleCommentList,
  getArticleEditorId,
  setArticleCommentList,
  setArticleCommentListRecommend,
} from '@/services';
import { IArticleCommentItem, IArticleEditorId } from '@/types';
import { Effect, Reducer } from 'umi';
import { formatting } from '@/utils';
export interface ArticleCommentModelState {
  articleCommentList: IArticleCommentItem[];
  articleCommentListLength: number;
  articleEditorItem: Partial<IArticleEditorId>;
  articleCommentListRecommend: Partial<IArticleEditorId>;
  aArticleCommentList:Partial<IArticleEditorId>;
  articleCommentListDel:Partial<IArticleEditorId>;
}

export interface ArticleCommentModelType {
  namespace: 'article';
  state: ArticleCommentModelState;
  effects: {
    getArticleCommentList: Effect;
    getArticleEditorId: Effect;
    setArticleCommentList: Effect;
    setArticleCommentListRecommend: Effect;
    delArticleCommentList: Effect;
  };
  reducers: {
    save: Reducer<ArticleCommentModelState>;
  };
}

const ArticleCommentModel: ArticleCommentModelType = {
  // 数据模型，用来隔离数据防止相互影响
  namespace: 'article',

  // 数据模型状态，相当于vuex的state
  state: {
    articleCommentList: [],
    articleCommentListLength: 0,
    articleEditorItem: {},
    articleCommentListRecommend: {},
    aArticleCommentList:{},
    articleCommentListDel:{}
  },

  // 异步操作，相当于vuex的action
  effects: {
    // 先简单记忆，*相当于async，yield相当于await
    *getArticleCommentList({ payload }, { call, put, select }) {
      let result = yield call(getArticleCommentList, payload);
      if (result.data) {
        yield put({
          type: 'save',
          payload: {
            articleCommentList: result.data[0].map((item: any) => {
              item.publishAt = formatting(item.publishAt);
              item.key = item.id;
              return item;
            }),
            pageLength: result.data[1],
          },
        });
      }
      if (result.data) {
        yield put({
          type: 'save',
          payload: {
            commentList: result.data[0],
            commentLength: result.data[1],
          },
        });
      }
    },
    *getArticleEditorId({ payload }, { call, put, select }) {
      let result = yield call(getArticleEditorId, payload);
      if (result.data) {
        yield put({
          type: 'save',
          payload: {
            articleEditorItem: result.data,
          },
        });
      }
    },
    *setArticleCommentList({ payload }, { call, put }) {
      let result = yield call(setArticleCommentList, payload);
      if (result.data) {
        yield put({
          type: 'save',
          payload: {
            aArticleCommentList: result.data,
          },
        });
      }
    },
    *setArticleCommentListRecommend({ payload }, { call, put }) {
      let result = yield call(setArticleCommentListRecommend, payload);
      if (result.data) {
        yield put({
          type: 'save',
          payload: {
            articleCommentListRecommend: result.data,
          },
        });
      }
    },
    *delArticleCommentList({ payload }, { call, put }) {
      let result = yield call(delArticleCommentList, payload);
      if (result.data) {
        yield put({
          type: 'save',
          payload: {
            articleCommentListDel: result.data,
          },
        });
      }
    }
  },

  // 同步操作，相当于vuex的mutation
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};

export default ArticleCommentModel;
